<?php
namespace Home\Controller;
use Think\Controller;
class NewsController extends Controller
{
    public function index()
    {
        if(session('username')){
            $this->assign('username',session('username'));
            $news = M('article');
            $news_list = $news->
            field(array('id','cid','wzname','author','createtime','lasttime'))
                ->select();
            $news_list = $this->filter($news_list);
//            var_dump($news_list);
//            exit();
            $this->assign('news_list',$news_list);
            $this->display();

        }else{
            //当用户没有登录时，跳转到登录页面
            $this->error("请先登录",U('/Home/Index/login/'));
        }

    }


    public function  filter($param){

        foreach ($param as $key => $value){
            $param[$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
            if(!$value['lasttime']){
                $param[$key]['lasttime'] = '无';
            }else{
                $param[$key]['lasttime'] = date('Y-m-d H:i:s',$value['lasttime']);
            }
            if(mb_strlen($value['wzname'],'utf-8') >8){
                $param[$key]['wzname'] = mb_substr($value['wzname'],0,8,'utf-8')."......";
            }

        }
        return $param;
    }
    public function quit(){
        if(session('username')){
            session(null);
        }

        redirect(U('/Home/Index/login/'),0,"重新登录");
    }
    public function  juns(){
        $cid=I("id");
redirect(U("/Home/Jun/index/",array('cid'=>$cid)));

    }
    public function  yul(){
        $cid=I("id");
        redirect(U("/Home/Yl/index/",array('cid'=>$cid)));

    }
    public function  china(){
        $cid=I("id");
        redirect(U("/Home/Ch/index/",array('cid'=>$cid)));

    }
    public function message(){
        $cid=I("id");
//        var_dump($cid);
//        exit();
        redirect(U("/Home/Message/index/",array('id'=>$cid)));
    }
}